// FORM VALIDATION FEEDBACK ICONS
// =================================================================
var faIcon = {
    valid: 'fa fa-check-circle fa-lg text-success',
    invalid: 'fa fa-times-circle fa-lg',
    validating: 'fa fa-refresh'
}


$('#customer-form').bootstrapValidator({
    excluded: [':disabled'],
    feedbackIcons: faIcon,
    fields: {
        name: {
            //container: 'popover',
            validators: {
                notEmpty: {
                    message: 'The title is required'
                }
            }
        },
        phone: {
            //container: 'popover',
            validators: {
                notEmpty: {
                    message: '电话号码不能为空'
                },
                phone: {
                    country: 'CN',
                    message: '电话号码格式不对'
                }
            }
        },
        url: {
            //container: 'popover',
            validators: {

                uri: {
                    message: '网址格式不对'
                }
            }
        }
    }
});

$("#submit").on("click", function () {
    var validator = $('#customer-form').data('bootstrapValidator');
    validator.validate();
    if (validator.isValid()) {

        var myform = $('#customer-form');

        // Find disabled inputs, and remove the "disabled" attribute
        var disabled = myform.find(':input:disabled').removeAttr('disabled');

        // serialize the form
        var serialized = myform.serialize();

        // re-disabled the set of inputs that you previously enabled
        disabled.attr('disabled','disabled');

        $.ajax({
            type: "post",
            url: "/admin/customer/edit",
            data: serialized,
            success: function (result) {
                bootbox.confirm({
                    message: "<p class='text-semibold text-main'>提示信息</p><p>修改成功,是否继续修改！</p>",
                    buttons: {
                        confirm: {
                            label: "继续修改"
                        },
                        cancel: {
                            label: "查看列表"
                        }
                    },
                    callback: function (result) {
                        if (!result) {
                            window.location.href = "/admin/customer/list";
                        }
                    },
                    animateIn: 'swing',
                    animateOut: 'hinge'
                });
            },
            error: function (result) {
                bootbox.confirm({
                    message: "<p class='text-semibold text-main'>提示信息</p><p>添加失败,请稍后重试！</p>",
                    buttons: {
                        confirm: {
                            label: "继续添加"
                        },
                        cancel: {
                            label: "查看列表"
                        }
                    },
                    callback: function (result) {
                        if (!result) {
                            window.location.href = "/admin/customer/list";
                        }
                    },
                    animateIn: 'swing',
                    animateOut: 'hinge'
                });
            }
        });
    }
});
$(function () {
    var dateTimePicker = $('#datetimepicker').datetimepicker({
        //inline: true,
        format: 'YYYY-MM-DD HH:mm:ss'
        //sideBySide: true
    });

    dateTimePicker.on('dp.change', function (e) {
        //console.log(JSON.stringify(e.date));
    });

    $("#enableAlert").change(function (e) {
        if(this.checked){
            $('#datetimepicker').datetimepicker("enable");
            $(this).val("1");
        }else {
            $('#datetimepicker').datetimepicker("disable");
            $(this).val("0");
        }
    });
});
